package com.work.music.dao;

import com.work.music.entity.MusicAlbumManager;
import com.work.music.entity.MusicLyricsManager;
import com.work.music.entity.MusicSongsterManager;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by xs on 2019/4/16.
 */
@Repository
public interface MusicManagerMapper {
    @Select({
            "select",
            "id, uid, name, headimg, synopsis, status, reason, addtime, addip",
            "from music_songster",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.BIGINT, id=true),
            @Result(column="uid", property="musicUser", jdbcType=JdbcType.BIGINT, many = @Many(select = "com.work.music.dao.MusicUserMapper.selectByPrimaryKey",fetchType= FetchType.LAZY)),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="headimg", property="headimg", jdbcType=JdbcType.VARCHAR),
            @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR)
    })
    MusicSongsterManager getMusicSongsterById(long id);

    /**
     * 获取总条数
     * @return
     */
    @Select("select ifnull(count(id), 0) from music_songster ${msg}")
    int selectSongsterCount(@Param("msg") String msg);

    /**
     * 获取歌手列表信息
     * @param msg
     * @param start
     * @param count
     * @return
     */
    @Select({
            "select",
            "id, uid, name, headimg, synopsis, status, reason, addtime, addip",
            "from music_songster",
            "${msg}",
            "limit #{start},#{count}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.BIGINT, id=true),
            @Result(column="uid", property="musicUser", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicUserMapper.selectByPrimaryKey")),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="headimg", property="headimg", jdbcType=JdbcType.VARCHAR),
            @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR)
    })
    List<MusicSongsterManager> selectSongsterList(@Param("msg") String msg, @Param("start") int start, @Param("count") int count);

    /**
     * 根据id获取专辑详情
     * @param id
     * @return
     */
    @Select({
            "select",
            "id, uid, songster_id, name, cover, synopsis, addtime, addip, status, reason",
            "from music_album",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="uid", property="musicUser", jdbcType=JdbcType.BIGINT,
                many = @Many(select = "com.work.music.dao.MusicUserMapper.selectByPrimaryKey")),
            @Result(column="songster_id", property="musicSongster", jdbcType=JdbcType.BIGINT,
                many = @Many(select = "com.work.music.dao.MusicSongsterMapper.selectByPrimaryKey")),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="cover", property="cover", jdbcType=JdbcType.VARCHAR),
            @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR)
    })
    MusicAlbumManager getAlbumById(long id);

    /**
     * 获取专辑列表信息
     * @param msg
     * @return
     */
    @Select("select ifnull(count(*), 0) from music_album ${msg}")
    int selectAlbumCount(@Param("msg") String msg);

    /**
     * 获取专辑列表信息
     * @param msg
     * @param start
     * @param count
     * @return
     */
    @Select({
            "select",
            "id, uid, songster_id, name, cover, synopsis, addtime, addip, status, reason",
            "from music_album",
            "${msg}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="uid", property="musicUser", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicUserMapper.selectByPrimaryKey")),
            @Result(column="songster_id", property="musicSongster", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicSongsterMapper.selectByPrimaryKey")),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="cover", property="cover", jdbcType=JdbcType.VARCHAR),
            @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR)
    })
    List<MusicAlbumManager> selectAlbumList(@Param("msg") String msg, @Param("start") int start, @Param("count") int count);

    @Select({
            "select",
            "lyr.id, lyr.uid, lyr.songster_id, lyr.type, lyr.album_id, lyr.name, lyr.cover, lyr.synopsis, lyr.addtime, lyr.addip,",
            // "id, uid, songster_id, type, album_id, name, cover, synopsis, addtime, addip, ",
            "lyr.status, lyr.reason, lyr.translate, usr.nick_name",
            // "status, reason, translate",
            "from music_lyrics lyr",
            "INNER JOIN music_user usr ON lyr.uid = usr.id",
            "where lyr.id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="uid", property="musicUser", jdbcType=JdbcType.BIGINT
                    ,many = @Many(select = "com.work.music.dao.MusicUserMapper.selectByPrimaryKey")),
            @Result(column="songster_id", property="musicSongster", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicSongsterMapper.selectByPrimaryKey")),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
            @Result(column="album_id", property="musicAlbum", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicAlbumMapper.selectByPrimaryKey")),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="cover", property="cover", jdbcType=JdbcType.VARCHAR),
            @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="translate", property="translate", jdbcType=JdbcType.VARCHAR),
            @Result(column="nick_name", property="nickName", jdbcType=JdbcType.VARCHAR)
    })
    MusicLyricsManager getLyricsById(long id);

    /**
     * 获取歌词总条数
     * @param msg
     * @return
     */
    @Select("select ifnull(count(*), 0) from music_lyrics")
    int selectLyricsCount(@Param("msg") String msg);

    /**
     * 获取歌词列表信息
     * @param msg
     * @param start
     * @param count
     * @return
     */
    @Select({
            "select",
            "id, uid, songster_id, type, album_id, name, cover, synopsis, addtime, addip, ",
            "status, reason, translate",
            "from music_lyrics",
            "${msg}",
            "order by addtime desc limit #{start},#{count}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="uid", property="musicUser", jdbcType=JdbcType.BIGINT
                    ,many = @Many(select = "com.work.music.dao.MusicUserMapper.selectByPrimaryKey")),
            @Result(column="songster_id", property="musicSongster", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicSongsterMapper.selectByPrimaryKey")),
            @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
            @Result(column="album_id", property="musicAlbum", jdbcType=JdbcType.BIGINT,
                    many = @Many(select = "com.work.music.dao.MusicAlbumMapper.selectByPrimaryKey")),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="cover", property="cover", jdbcType=JdbcType.VARCHAR),
            @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.VARCHAR),
            @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="translate", property="translate", jdbcType=JdbcType.VARCHAR)
    })
    List<MusicLyricsManager> selectLyricsList(@Param("msg") String msg, @Param("start") int start, @Param("count") int count);
}
